package com.naodong.shenluntiku.mvp.view.widget;

import android.content.Context;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.widget.NestedScrollView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.naodong.shenluntiku.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hujie on 16/5/17.
 */
public class BottomSheetListView {


    private Context context;
    private BottomSheetDialog dialog;
    private BottomSheetListener listener;
    private boolean mutilSelectMode = false;

    private List<String> selectStrings;


    public BottomSheetListView(Context context) {
        this.context = context;
    }


    public void setMutilSelectMode(boolean mutilSelectMode, List<String> selectStrings) {
        this.mutilSelectMode = mutilSelectMode;
        if (selectStrings == null) {
            this.selectStrings = new ArrayList<>();
        } else {
            this.selectStrings = selectStrings;
        }
    }

    public void setOtherButtonTitles(final String... titles) {
        dialog = new BottomSheetDialog(context);

        View view = LayoutInflater.from(context).inflate(
                R.layout.bottom_sheet_list_layout, null, false);

        View sureBtn = view.findViewById(R.id.sureBtn);
        View cancleBtn = view.findViewById(R.id.cancleBtn);

        dialog.setContentView(view);


        sureBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener != null) {
                    listener.onMutilButtonClick(selectStrings);
                }
            }
        });

        if (mutilSelectMode) {
            sureBtn.setVisibility(View.VISIBLE);
        }
        LinearLayout contentView = (LinearLayout) view.findViewById(R.id.contentView);
        NestedScrollView nestedScrollView = (NestedScrollView) view.findViewById(R.id.nestedScrollView);
        for (int i = 0; i < titles.length; i++) {
            View item = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_list_item, null);
            ImageView checkBox = (ImageView) item.findViewById(R.id.checkbox);
            if (mutilSelectMode) {
                if (selectStrings != null && selectStrings.contains(titles[i])) {
                    checkBox.setSelected(true);
                } else {
                    checkBox.setSelected(false);
                }
                checkBox.setVisibility(View.VISIBLE);
            } else {
                checkBox.setVisibility(View.GONE);
            }
            TextView textView = (TextView) item.findViewById(R.id.tv);
            textView.setText(titles[i]);
            final int finalI = i;
            item.setOnClickListener(v -> {
                if (!mutilSelectMode) {
                    if (listener != null) {
                        dismiss();
                        listener.onOtherButtonClick(titles[finalI], finalI);
                    }
                } else {
                    if (listener != null && listener.checkCanSelect(selectStrings, titles[finalI], finalI)) {
                        if (selectStrings != null && selectStrings.contains(titles[finalI])) {
                            checkBox.setSelected(false);
                            selectStrings.remove(titles[finalI]);
                        } else {
                            checkBox.setSelected(true);
                            selectStrings.add(titles[finalI]);
                        }
                    }
                }
            });
            contentView.addView(item);
        }
        calculateContentViewLayout(nestedScrollView, titles.length);
        expendAll(view);

    }

    private void calculateContentViewLayout(NestedScrollView nestedScrollView, int length) {

        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) nestedScrollView.getLayoutParams();

        if (length > 10) {
            lp.height = dip2px(context, 45) * 10;
        }
        nestedScrollView.setLayoutParams(lp);

    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    private void expendAll(View contentView) {
        View parent = (View) contentView.getParent();
        BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
        contentView.measure(0, 0);
        behavior.setPeekHeight(2000);

        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) parent.getLayoutParams();
        params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
        parent.setLayoutParams(params);
    }


    public interface BottomSheetListener {
        void onOtherButtonClick(String result, int index);

        void onMutilButtonClick(List<String> selectStrings);

        boolean checkCanSelect(List<String> selectStrings, String result, int index);

    }

    public static class SimpleBottomSheetListener implements BottomSheetListener{

        @Override
        public void onOtherButtonClick(String result, int index) {

        }

        @Override
        public void onMutilButtonClick(List<String> selectStrings) {

        }

        @Override
        public boolean checkCanSelect(List<String> selectStrings, String result, int index) {
            return false;
        }
    }


    public void setListener(BottomSheetListener listener) {
        this.listener = listener;
    }


    public void dismiss() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    public void show() {
        if (dialog != null) {
            dialog.show();
        }
    }


}
